package org.mspring.mlog.web.module.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mspring.mlog.common.PageNames;
import org.mspring.mlog.entity.Post;
import org.mspring.mlog.service.PostService;
import org.mspring.mlog.web.query.PostQueryCriterion;
import org.mspring.platform.dao.support.Order;
import org.mspring.platform.dao.support.Order.OrderType;
import org.mspring.platform.dao.support.Page;
import org.mspring.platform.web.freemarker.FreemarkerVariableNames;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 
 * @author Gao Youbo
 * @since 2012-7-23
 */
@Widget
public class Web_IndexWidget extends Web_AbstractWidget {
    @Autowired
    private PostService postService;

    /**
     * 文章首页
     * 
     * @param postPage
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping({ "/", "" })
    public String index(@ModelAttribute Page<Post> postPage, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (postPage == null) {
            postPage = new Page<Post>();
        }
        postPage.setSort(new Order("createTime", OrderType.DESC));
        Map<String, Object> queryParams = new HashMap<>();
        queryParams.put("status", Post.Status.PUBLISH);
        postService.listPage(new PostQueryCriterion(queryParams), postPage);
        model.addAttribute(FreemarkerVariableNames.POST_PAGE, postPage);
        setCurrnetPage(model, PageNames.INDEX);
        return "skin:/index";
    }
}
